import { getRoleList, createRole, updateRole, getAllMenusByRoleId, updateResource, getRoleListByCreateUser } from '../services/api';
import { message } from 'antd';
import { isResponerCode } from '../utils/utils';
export default {
  namespace: 'role',

  state: {
    roleList: [],
    roleLoading: false,
    roleResourceLoading: false,
    rolePage: {},
    roleResult: null,
    resourcesList: [],
    roleListByUser: null,
  },

  effects: {
    *fetchRole(_, { call, put }) {
      yield put({
        type: 'show',
        payload: {
          roleLoading: true,
        },
      });
      const response = yield call(getRoleList);
      if (!isResponerCode(response)) {
        yield put({
          type: 'show',
          payload: {
            roleLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'show',
        payload: {
          roleLoading: false,
        },
      });
      yield put({
        type: 'show',
        payload: {
          roleList: response.data,
        }
      });
    },

    *getRoleListByCreateUser(_, { call, put }) {
      yield put({
        type: 'show',
        payload: {
          roleLoading: true,
        },
      });
      const response = yield call(getRoleListByCreateUser);
      if (!isResponerCode(response)) {
        yield put({
          type: 'show',
          payload: {
            roleLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'show',
        payload: {
          roleLoading: false,
        },
      });
      yield put({
        type: 'show',
        payload: {
          roleListByUser: response.data,
        }
      });
    },

    //添加角色
    *createRole({ payload, callback }, { call, put }) {
      yield put({
        type: 'show',
        payload: {
          roleResourceLoading: false,
        },
      });
      const response = yield call(createRole, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'show',
          payload: {
            roleResourceLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'show',
        payload: {
          roleResourceLoading: false,
        },
      });
      yield put({
        type: 'show',
        payload: {
          data: response,
          roleResult: response.result,
        },
      });
      if (callback) callback();
    },

    //修改角色信息
    *updateRole({ payload, callback }, { call, put }) {
      yield put({
        type: 'show',
        payload: {
          roleResourceLoading: false,
        },
      });
      const response = yield call(updateRole, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'show',
          payload: {
            roleResourceLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'show',
        payload: {
          roleResourceLoading: false,
        },
      });
      yield put({
        type: 'show',
        payload: {
          data: response,
          roleResult: response.result,
        },
      });
      if (callback) callback();
    },

    *getAllMenusByRoleId({ payload, callback }, { call, put }) {
      const response = yield call(getAllMenusByRoleId, payload);
      if (!isResponerCode(response)) {
        return false
      }
      yield put({
        type: 'show',
        payload: {
          resourcesList: response.data
        },
      });
      if (callback) callback();
    },
    *updateResourceId({ payload, callback }, { call, put }) {
      yield put({
        type: 'show',
        payload: {
          roleResourceLoading: true,
        },
      });
      const response = yield call(updateResource, payload);
      if (!isResponerCode(response)) {
        yield put({
          type: 'show',
          payload: {
            roleResourceLoading: false,
          },
        });
        return false
      }
      yield put({
        type: 'show',
        payload: {
          roleResourceLoading: false,
        },
      });
      yield put({
        type: 'show',
        payload: {
          data: response.data,
          roleResult: response.result
        }
      });
      if (callback) callback();
    },

    // //删除组织
    // *deleteStructure({ payload, callback }, { call, put }) {
    //   yield put({
    //     type: 'show',
    //     payload: {
    //       streuctureLoading: true,
    //     },
    //   });
    //   const response = yield call(deleteStructure, payload);
    //   yield put({
    //     type: 'show',
    //     payload: {
    //       streuctureLoading: false,
    //     },
    //   });
    //   if (response.result.code == '200') {
    //     message.success("提交成功")
    //   } else {
    //     message.error(response.result.message)
    //   }
    //   yield put({
    //     type: 'show',
    //     payload: {
    //       data: response.data,
    //     },
    //   });
    //   if (callback) callback();
    // },
    // *updateAllMenusByRoleId({ payload }, { call, put }) {
    //   console.log("payload",payload)
    //   const response = yield call(updateResource, payload);
    //   console.log("response",response)

    //   yield put({
    //     type: 'query',
    //     payload: {

    //     },
    //   });
    //   if(callback)callback();
    // },

  },

  reducers: {
    show(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
